package org.itboys.core.controller.front.invoice;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.service.mongo.front.SundayFrontInvoiceService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：贾琦磊
 * @Date：2018/11/27
 * @Description：发票
 */
@Controller
@Api(value = "invoice", description = "发票")
@RequestMapping("sunday/mobile/invoice")
public class SundayFrontInvoiceController extends BaseController {

    @Autowired
    private SundayFrontInvoiceService invoiceService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "发票信息提交")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "订单id集合用,分隔") @RequestParam(value = "orderIds", required = true) List<Long> orderIds
            , @ApiParam(value = "类型：1--电子 2--纸质") @RequestParam(value = "type", required = false) Integer type
            , @ApiParam(value = "抬头类型：1--个人 2--企业") @RequestParam(value = "headType", required = false) Integer headType
            , @ApiParam(value = "抬头") @RequestParam(value = "head", required = false) String head
            , @ApiParam(value = "税号") @RequestParam(value = "dutyNo", required = false) String dutyNo
            , @ApiParam(value = "金额") @RequestParam(value = "money", required = false) String money
            , @ApiParam(value = "邮箱") @RequestParam(value = "email", required = false) String email
            , @ApiParam(value = "姓名") @RequestParam(value = "name", required = false) String name
            , @ApiParam(value = "电话") @RequestParam(value = "mobile", required = false) String mobile
            , @ApiParam(value = "地区") @RequestParam(value = "area", required = false) String area
            , @ApiParam(value = "地址") @RequestParam(value = "address", required = false) String address) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        invoiceService.save(orderIds, memberId, type, headType, head, dutyNo, money, email, name, mobile, area, address);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @ApiOperation(value = "开票历史")
    public void select(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = false) String pageSize
            , @ApiParam(value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = false) String pageNumber
            , @ApiParam(value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort
            , @ApiParam(value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        ResponseMessage.success(invoiceService.select(request, memberId));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation("发票详情")
    public void select(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "发票id", required = true) @RequestParam(value = "id") Long id) {
        ResponseMessage.success(invoiceService.detail(id));
    }

    @RequestMapping(value = "/selectOrders", method = RequestMethod.POST)
    @ApiOperation(value = "按订单开发票", notes = "搜索可以开具发票的订单")
    public void selectOrders(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = false) String pageSize
            , @ApiParam(value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = false) String pageNumber
            , @ApiParam(value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort
            , @ApiParam(value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        ResponseMessage.success(invoiceService.getOrders(request, memberId));
    }
}
